/**
 *
 * @param num
 * @returns {string}
 */
// 基础写法
function convert(num) {
    var sig = 0,ten = 0,hundred = 0,thousand = 0;
    var str = '';
    sig = num % 10;
    if(num >= 10)  ten = Math.floor(num / 10 % 10);
    if(num >= 100)  hundred = Math.floor(num / 100 % 10);
    if(num >= 1000)  thousand = Math.floor(num / 1000);
    var count = {
        thousand : thousand, hundred : hundred, ten: ten,sig : sig
    };
    //console.log(count);

    for(var i in count){
        switch(i){
            case 'thousand':
                str += jisuan('M',count[i]);
                break;
            case 'hundred':
                str += jisuan('C',count[i]);
                break;
            case 'ten':
                str += jisuan('X',count[i]);
                break;
            case 'sig':
                str += jisuan('I',count[i]);
                break;
        }
    }

    return str;
}

function jisuan(str,num){
    if(num === 0) return '';
    if(str == 'M'){
        switch(num){
            case 1: return 'M';
            case 2: return 'MM';
            case 3: return 'MMM';
            default:
                return '';
        }
    }
    if(str == 'C'){
        switch(num){
            case 1: return 'C';
            case 2: return 'CC';
            case 3: return 'CCC';
            case 4: return 'CD';
            case 5: return 'D';
            case 6: return 'DC';
            case 7: return 'DCC';
            case 8: return 'DCCC';
            case 9: return 'CM';
            default:
                return '';
        }
    }
    if(str == 'X'){
        switch(num){
            case 1: return 'X';
            case 2: return 'XX';
            case 3: return 'XXX';
            case 4: return 'XL';
            case 5: return 'L';
            case 6: return 'LX';
            case 7: return 'LXX';
            case 8: return 'LXXX';
            case 9: return 'XC';
            default:
                return '';
        }
    }
    if(str == 'I'){
        switch(num){
            case 1: return 'I';
            case 2: return 'II';
            case 3: return 'III';
            case 4: return 'IV';
            case 5: return 'V';
            case 6: return 'VI';
            case 7: return 'VII';
            case 8: return 'VIII';
            case 9: return 'IX';
            default:
                return '';
        }
    }
}


convert(36);
//console.log(convert(36));


// 高级写法
/**
* 将1,10,40，50等等分割数输入到一个数组中，用另一个数组记录相对应的罗马字符。
* 从最大的数字开始遍历，如果num大于当前数，则减去当前数，继续进入循环。字符串则加上对应的罗马字符。
*/
function convert2(num) {
    var numArr = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
    var strArr = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];
    var result = "";
    while (num > 0) {
        var i = numArr.length;
        while (i >= 0) {
            if (num >= numArr[i]) {
                result += strArr[i];
                num -= numArr[i];
            } else {
                i--;
            }
        }
    }
    return result;
}

console.log(convert2(36));